package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* StepOnView
* */
class StepOnView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mCornerValue = context.resources.getDimension(R.dimen.dp_3)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            pathEffect = CornerPathEffect(mCornerValue)
            color = context.resources.getColor(R.color.color_FF5252)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 15).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth

            mPath0.apply {
                reset()
                moveTo(-cx * 40 / 100, cy * 20 / 100)
                lineTo(-cx * 90 / 100, cy * 20 / 100)
                lineTo(-cx * 90 / 100, -cy * 90 / 100)
                lineTo(-cx * 40 / 100, -cy * 90 / 100)
            }
            drawPath(mPath0, mPaint0)

            mPath0.apply {
                reset()
                moveTo(-cx * 40 / 100, -cy * 90 / 100)
                lineTo(-cx * 40 / 100, cy * 20 / 100)
            }
            drawPath(mPath0, mPaint0)

            mPath0.apply {
                reset()
                moveTo(-cx * 40 / 100, -cy * 90 / 100)
                lineTo(cx * 65 / 100, -cy * 90 / 100)
                quadTo(cx * 80 / 100,-cy * 50 / 100,cx * 90 / 100, cy * 20 / 100)
                lineTo(cx * 10 / 100, cy * 20 / 100)
                quadTo(cx * 34 / 100, cy * 70 / 100, cx * 10 / 100, cy * 90 / 100)
                quadTo(-cx * 5 / 100, cy * 45 / 100, -cx * 40 / 100, cy * 20 / 100)
            }
            drawPath(mPath0, mPaint0)

            restore()
        }
    }
}